﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Security.Cryptography;
using System.IO;
using System.Management;
namespace BanHangCshap
{
    public static class UltitityStatic
    {
        public static string ConvertNumberToText(string s)
        {
            string tempNum2Text = null;
            try
            {
                string[] so = { "không", "một", "hai", "ba", "bốn", "năm", "sáu", "bảy", "tám", "chín" };
                string[] hang = { "", "nghìn", "triệu", "tỷ" };

                int i = 0;
                int j = 0;
                int donvi = 0;
                int chuc = 0;
                int tram = 0;
                string str = null;
                str = "";
                i = s.Length;

                for (j = 0; j < i; j++)
                {
                    if (s.Substring(0, 1) == "0")
                    {
                        s = s.Substring(s.Length - (i - j));
                    }
                }
                if (s == "0")
                {
                    return so[0];
                }
                i = s.Length;
                if (i == 0)
                {
                    str = "";
                }
                else
                {
                    j = 0;
                    while (i > 0)
                    {
                        donvi = int.Parse(s.Substring(i - 1, 1));
                        i = i - 1;
                        if (i > 0)
                        {
                            chuc = int.Parse(s.Substring(i - 1, 1));
                        }
                        else
                        {
                            chuc = -1;
                        }
                        i = i - 1;
                        if (i > 0)
                        {
                            tram = int.Parse(s.Substring(i - 1, 1));
                        }
                        else
                        {
                            tram = -1;
                        }
                        i = i - 1;
                        if (donvi > 0 | chuc > 0 | tram > 0 || j == 3)
                        {
                            str = hang[j] + " " + str;
                        }
                        j = j + 1;
                        if (j > 3)
                        {
                            j = 1;
                        }
                        if (donvi == 1 && chuc > 1)
                        {
                            str = "mốt" + " " + str;
                        }
                        else
                        {
                            if (donvi == 5 && chuc > 0)
                            {
                                str = "lăm" + " " + str;
                            }
                            else if (donvi > 0)
                            {
                                str = so[donvi] + " " + str;
                            }
                        }
                        if (chuc < 0)
                        {
                            break;
                        }
                        else
                        {
                            if (chuc == 0 && donvi > 0)
                            {
                                str = "lẻ" + " " + str;
                            }
                            else if (chuc == 1)
                            {
                                str = "mười" + " " + str;
                            }
                            else if (chuc > 1)
                            {
                                str = so[chuc] + " " + "mươi" + " " + str;
                            }
                        }
                        if (tram < 0)
                        {
                            break;
                        }
                        else
                        {
                            if (tram > 0 | chuc > 0 | donvi > 0)
                            {
                                str = so[tram] + " " + "trăm" + " " + str;
                            }
                        }
                    }
                }
                str = str.Trim(' ');
                str = str.Substring(0, 1).ToUpper() + str.Substring(str.Length - (str.Length - 1));
                tempNum2Text = "(" + str + " đồng)";
            }
            catch (Exception)
            {
                tempNum2Text = "";
            }
            return tempNum2Text;
        }
        public static void formatNumber(DevExpress.XtraEditors.TextEdit txt)
        {
            try
            {
                double number = double.Parse(txt.Text.Replace(",", "").Replace(".", ""));
                txt.Text = number.ToString("##,##0.##");
               
                txt.SelectionStart = txt.Text.Length;
            }
            catch (Exception)
            {


            }
        }
        public static void thongBaoLoi(string message)
        {
            XtraMessageBox.Show(message, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        public static void thongBaoNhacNho(string message)
        {
            XtraMessageBox.Show(message, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Question);
        }
        public static void thongBaoThanhCong(string message)
        {
            XtraMessageBox.Show(message, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        public static DateTime layNgayDauThang()
        {
            string sdate = DateTime.Now.ToString("dd/MM/yyyy");
            string sthang = sdate.Substring(3,2);
            return DateTime.ParseExact("01/" + sthang + "/" + DateTime.Now.Year.ToString(), "dd/MM/yyyy", null);
        }
        public static byte[] ReadFile(string sPath)
        {
            //Initialize byte array with a null value initially.
            byte[] data = null;

            //Use FileInfo object to get file size.
            FileInfo fInfo = new FileInfo(sPath);
            long numBytes = fInfo.Length;

            //Open FileStream to read file
            FileStream fStream = new FileStream(sPath, FileMode.Open, FileAccess.Read);

            //Use BinaryReader to read file stream into byte array.
            BinaryReader br = new BinaryReader(fStream);

            //When you use BinaryReader, you need to supply number of bytes to read from file.
            //In this case we want to read entire file. So supplying total number of bytes.
            data = br.ReadBytes((int)numBytes);
            return data;
        }
        public static bool IsValidInt(string val)
        {
            return System.Text.RegularExpressions.Regex.IsMatch(val, @"^[1-9]\d*\.?[0]*$");
        }
        public static string maHoaBase64(byte[] Bydata)
        {
            if (Bydata == null)
            {
                throw new ArgumentNullException("Bydata");
            }
            return Convert.ToBase64String(Bydata);
        }

        public static byte[] giaiMaBase64(string base64)
        {
            if (base64 == null)
            {
                throw new ArgumentNullException("base64");
            }
            return Convert.FromBase64String(base64);
        }
        public static string MD5(string chuoi_nguon)
        {
            ASCIIEncoding ASCIIenc = new ASCIIEncoding();
            string strReturn = "";
            byte[] ByteSourceText = ASCIIenc.GetBytes(chuoi_nguon);
            MD5CryptoServiceProvider Md5Hash = new MD5CryptoServiceProvider();
            byte[] ByteHash = Md5Hash.ComputeHash(ByteSourceText);
            foreach (byte b in ByteHash)
            {
                strReturn += b.ToString("x2");
            }
            return strReturn;
        }
        public static Boolean deleteFile(string filename)
        {
            try
            {
                File.Delete(filename);
                return true;
            }
            catch
            {
                return false;
            }
        }
        public static void downloadFile(string filename)
        {
            try
            {
                FileInfo fi = new FileInfo("temp\\" + filename);
                FolderBrowserDialog open = new FolderBrowserDialog();

                open.ShowDialog();
                string path = "";
                if (open.SelectedPath.Equals("") == false)
                {
                    path = open.SelectedPath + "\\" + filename;
                    File.Delete(path);


                    fi.CopyTo(path);
                    if (XtraMessageBox.Show("Đã lưu thành công. Bạn có muốn mở không?", "Tải biểu mẫu", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        System.Diagnostics.Process.Start(path);
                    }

                }


            }
            catch (Exception)
            {
                XtraMessageBox.Show("Biển mẫu không tồn tại", "Tải biểu mẫu", MessageBoxButtons.OK, MessageBoxIcon.Error);

            }
        }
        public static string HardSerial()
        {
            string SerialNo = "";
            try
            {

                System.Management.ManagementObjectSearcher searcher = new System.Management.ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia");
                foreach (System.Management.ManagementObject wmi_HD in searcher.Get())
                {
                    if (wmi_HD["SerialNumber"].Equals("NULL") == true)
                    {
                        SerialNo = "None";
                    }
                    else
                    {
                        SerialNo = wmi_HD["SerialNumber"].ToString();
                    }
                    break;
                }
            }
            catch (Exception)
            {
                try
                {
                    string cpuInfo = string.Empty;
                    string temp = string.Empty;
                    ManagementClass mc = new ManagementClass("Win32_Processor");
                    ManagementObjectCollection moc = mc.GetInstances();
                    foreach (ManagementObject mo in moc)
                    {
                        if (cpuInfo == string.Empty)
                        {
                            // only return cpuInfo from first CPU
                            cpuInfo = mo.Properties["ProcessorId"].Value.ToString();
                            Console.WriteLine(("CPU ID : " + cpuInfo));
                        }
                    }
                    SerialNo = cpuInfo;
                }
                catch (Exception)
                {
                }
            }
            return SerialNo;
        }
   
    }
}
